package com.lby.graduation.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lby.graduation.entity.Message;
import com.lby.graduation.entity.User;
import com.lby.graduation.mapper.MessageMapper;
import com.lby.graduation.service.MessageService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李兵耀
 * @since 2021-04-23
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private MessageService messageService;
    @Resource
    private MessageMapper messageMapper;
    /*用户查找公告*/
    @GetMapping("/select")
    public List<Message> select() {
        return messageService.list();
    }
    /*查找公告*/
    @GetMapping("/selectMessage")
    public Map selectMessage(Integer current, Integer size) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Message> messagePage = new Page<>(current, size);
        List<Message> list = messageMapper.selectMessage(messagePage);
        long total = messagePage.getTotal();
        map.put("code", 200);
        map.put("data", list);
        map.put("msg", "查找成功");
        map.put("count", total);
        return map;
    }

    /*发布公告*/
    @GetMapping("/addMessage")
    public boolean addMessage(Message message) {
        return messageService.save(message);
    }

    /*更新公告*/
    @GetMapping("/updateMessage")
    public boolean updateMessage(Message message) {
        return messageService.updateById(message);
    }

    /*删除公告*/
    @RequestMapping("/deleteMessage")
    public Map delectMessage(Integer messageId) {
        HashMap<String, Object> map = new HashMap<>();
        boolean b = messageService.removeById(messageId);
        if (b = true) {
            map.put("code", 200);
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }


}
